package top.malaoshi.webmvc;

import top.malaoshi.webmvc.info.MethodInfo;
import top.malaoshi.webmvc.info.MethodsArgumentInfo;
import top.malaoshi.webmvc.parameter.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * 对 ExceptionHandle 注解修饰方法进行解析形参
 */
public class ExceptionHandlerMethodArgumentResolver {

    static NoneResolver noneResolver = new NoneResolver();
    static HttpRequestResolver requestResolver = new HttpRequestResolver();
    static HttpResponseResolver responseResolver = new HttpResponseResolver();
    static HttpSessionResolver sessionResolver = new HttpSessionResolver();

    static ExceptionHandlerResolver exceptionHandlerResolver = new ExceptionHandlerResolver();


    public static void handler(Method method, MethodInfo mi) throws IOException {


        Class<?>[] parameterTypes = method.getParameterTypes();

        String[] names = MethodArgumentNameResolver2.handle(method);

        for(int i = 0,len = parameterTypes.length;i<len;i++){
            MethodsArgumentInfo mai = MethodsArgumentInfo.of(names[i],parameterTypes[i],i,mi);

            if(parameterTypes[i] == HttpServletRequest.class){
                mi.addArgumentResolvers(requestResolver);
            }else if(parameterTypes[i] == HttpServletResponse.class){
                mi.addArgumentResolvers(responseResolver);
            }else if(parameterTypes[i] == HttpSession.class){
                mi.addArgumentResolvers(sessionResolver);
            }else if(Throwable.class.isAssignableFrom(parameterTypes[i])){
                mi.addArgumentResolvers(exceptionHandlerResolver);
            }else{

            }
            mi.addMethodsArgumentInfo(mai);
        }
    }
}
